home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7524 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  62 lines

  1. Path: news.us.net!usenet
  2. From: thoth256@us.net (Evelio Perez-Albuerne)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Derived class not calling overloaded base class function
  5. Date: Fri, 23 Feb 1996 20:02:49 GMT
  6. Organization: US Net
  7. Message-ID: <4gl6d7$7jg@news.us.net>
  8. References: <4g46t2$3vd@otis.netspace.net.au> <fcusack-1902960717170001@mudskipper.cac.psu.edu>
  9. NNTP-Posting-Host: thoth256.laurel.us.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. In article <4g46t2$3vd@otis.netspace.net.au>, TorrBoy@netspace.net.au
  13. wrote:
  14.  
  15. > Hi,
  16. > Hopefully a very simple question someone can answer for me!
  17. > I have a class, say 'foo' (everyone's favourite) which has a member function 
  18. > 'int Get(void)'
  19. > I then derive a class (say 'goo') which has as a base class foo. If goo has a 
  20. > function also called Get, but with different params (say ''char* Get(char *)') I can't 
  21. > seem to get the base class' Get() function to operate within the derived class.
  22. > If I say just plain "Get()" it says too few parameters (for Get(char *)".
  23. >   If I use 
  24. > "::Get()" the compiler complains Get should have a prototype.
  25. >   If I say "foo.Get()" it 
  26. > says improper use of typedef foo.
  27.  
  28. Although this behavior - a function in a derived class hides all
  29. functions in base classes with the same name *even* if they have
  30. different parameters - seems illogical it is correct as per the ARM.
  31. See Scott Meyer's "Effective C++" book for an explanation of why this
  32. was decided.
  33.  
  34. To use the original int Get() function you need to redefine it in the
  35. derived class. Fortunately, the implementation is trivial (just call
  36. the base class function) and by using inlining, the compiler can
  37. optimize the additional function call away:
  38.  
  39. class foo
  40. {
  41.     // other stuff
  42.   public:
  43.     int Get();
  44. };
  45.  
  46. class goo : public foo
  47. {
  48.     // other stuff
  49.   public:
  50.     char* Get(char*);
  51.     int Get() { return foo::Get(); }
  52. };
  53.  
  54.  
  55. ********************************************************
  56. Evelio Perez-Albuerne <thoth256@us.net>
  57.  
  58.